<?php
namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Zend\Mvc\MvcEvent;

class RequestController extends AbstractActionController{

	public $view;
	public $session;
	protected $em;
	protected $authService;

	public function __construct(){
		$this->view = new ViewModel();
		$this->session = new Container();
		$this->em = $this->getEventManager();
        $this->em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'),100);
		echo "<script> var controllername='request'; </script>";
	}

	public function preDispatch(){
        if(!$this->getAuthService()->hasIdentity())
            $this->_redirect()->toRoute(NULL,array('controller'=>'auth','action'=>'login'));
        $username = $this->getAuthService()->getStorage()->read();
        $this->layout('layout/admin');
        $this->layout()->username = $username;
    }

    public function getAuthService(){
        if(!$this->authService){
            $this->authService = $this->getServiceLocator()->get('AuthService');
        }
        return $this->authService;
    }

	public function indexAction(){
		return $this->view;
	}
}